package com.woniu.job.controller;

import com.woniu.job.entity.Admin;
import com.woniu.job.entity.Category;
import com.woniu.job.entity.vo.ResponseVO;
import com.woniu.job.entity.vo.ResultVO;
import com.woniu.job.service.ICategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author sunnylausong
 *
 * 2019/12/24
 * 20:07
 */


@RestController
@RequestMapping("/update")
public class UpdateController {
    @Value("${files.location}")
    String path;

    @RequestMapping({"/photo"})
    public ResultVO uploadPermit(@RequestParam("file") MultipartFile[] files) {
        ResultVO responseVO = new ResultVO();
        MultipartFile[] var4 = files;
        int var5 = files.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            MultipartFile file = var4[var6];
            if (file.isEmpty()) {
                responseVO.setCode(500);
                responseVO.setMessage("请上传正确的文件格式");
            } else {
                //得到文件名
                String filName = file.getOriginalFilename();
                //取出文件后缀
                String suffix = filName.substring(filName.lastIndexOf("."));
                //存入文件地址
                String perms = UUID.randomUUID() + suffix;
                String paths = path + perms;
                File newFile = new File(paths);
                // 如果pathAll路径不存在，则创建相关该路径涉及的文件夹;
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                try {
                    //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
                    file.transferTo(newFile);
                    responseVO.setResult(true);
                    responseVO.setCode(200);
                    responseVO.setMessage("成功");
                    responseVO.setData("/files/" + perms);
                } catch (IOException var13) {
                    responseVO.setCode(-500);
                    responseVO.setMessage("失败");
                }
            }
        }

        return responseVO;
    }


}
